import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/yhpc/YhpcViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/StringUtils.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';

class ChoosePcxView extends BaseStatefulWidget {
  var pcdxId;
  ChoosePcxView(this.pcdxId);
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    // throw UnimplementedError();
    return ChoosePcxViewState();
  }
}

class ChoosePcxViewState extends BaseState<YhpcViewModel, ChoosePcxView> {
  TextEditingController _controller = TextEditingController(text: '');
  List _pcxList;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  _getData() {
    var map = {'riskId': StringUtils.getStr(widget.pcdxId), 'current': '1', 'size': '9999'};
    viewModel.getPcnrData(map);
    viewModel.getPcnrDataStream().listen((event) {
      if (event['code'] == '0') {
        setState(() {
          _pcxList = [];
          _pcxList = event['data']['records'];
        });
      } else {
        ToastUtils.showToast(event['msg']);
      }
    }, onError: (e) {
      print(e);
    });
  }

  @override
  Widget initWidget() {
    return Scaffold(
      backgroundColor: Color(0xfff4f5f6),
      appBar: MTopBar(
        elevation: 0,
        title: '选择排查项',
      ).create(),
      body: _pcxList == null
          ? LoadingWidget()
          : _pcxList.length == 0
              ? NoneData()
              : _bodyView(),
    );
  }

  _bodyView() {
    return Container(
            margin: EdgeInsets.all(Dimens.dimens16),
            child: SingleChildScrollView(
              child: ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: _pcxList.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        Navigator.pop(context, _pcxList[index]);
                      },
                      child: Container(
                        padding: EdgeInsets.all(10),
                        decoration: BoxDecoration(
                          color: SQColor.white,
                          borderRadius: BorderRadius.circular(6),
                        ),
                        child: JText(_pcxList[index]['dangerSourceName'] ?? ''),
                      ),
                    );
                  }),
            ),
          );
  }
}
